﻿using System;
using System.Collections;
using System.Collections.Specialized;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;

namespace FlexiCache
{
    /// <summary>
    /// This class provides a public API to access partial functionality of output cache that is based on custom MongoDB or SQL-Server provider.
    /// </summary>
    public sealed class ExternalOutputCacheApi
    {
        /// <summary>
        /// Method removes cached data from the output cache using specified meta tags.
        /// </summary>
        /// <remarks>If all parameters are empty, the the request is ignored.</remarks>
        /// <param name="path">The relative path used to request the data; should not include the query string. Can be empty or null.</param>
        /// <param name="metaTags">The collection of metatags to locate the data.</param>
        public static void RemoveOutputCacheItem(string path, string[] metaTags)
        {
            ExternalOutputCacheProviderHost.Provider.Remove(path, metaTags);
        }

        /// <summary>
        /// Method removes cached data from the output cache using specified meta tags.
        /// </summary>
        /// <remarks>If metaTags array is empty, the the request is ignored.</remarks>
        /// <param name="metaTags">The collection of metatags to locate the data.</param>
        public static void RemoveOutputCacheItem(string[] metaTags)
        {
            RemoveOutputCacheItem(null, metaTags);
        }

        /// <summary>
        /// Method removes cached data from the output cache using specified path and parameter as meta tags.
        /// </summary>
        /// <remarks>If both, path and parameter are empty, the the request is ignored.</remarks>
        /// <param name="path">The relative path used to request the data; should not include the query string. Can be empty or null.</param>
        /// <param name="parameterName">The name of parameter that should be used to build metatags to locate the data. This cannot be empty, otherwise is ignored.</param>
        /// <param name="parameterValue">The name of parameter that should be used to build metatags to locate the data. This can be empty or null.</param>
        public static void RemoveOutputCacheItem(string path, string parameterName, string parameterValue)
        {
            if (CachingConfiguration.Current.CustomOutputCacheConfig.IsEnabled)
            {
                string firstTag = (string.IsNullOrWhiteSpace(parameterName)) ? "" : string.Format("{0}={1}", parameterName, parameterValue == null ? "" : parameterValue);
                RemoveOutputCacheItem(path, new string[] { firstTag });
            }
        }

        /// <summary>
        /// Method removes cached data from the output cache using specified parameters as meta tags.
        /// </summary>
        /// <remarks>If both, path and parameters are empty, the the request is ignored.</remarks>
        /// <param name="path">The relative path used to request the data; should not include the query string. Can be empty or null.</param>
        /// <param name="parametersCollection">The collection of name-value parameters that should be used to build metatags to locate the data. 
        /// The metatags are built using name=value pattern.</param>
        public static void RemoveOutputCacheItem(string path, NameValueCollection parametersCollection)
        {
            if (CachingConfiguration.Current.CustomOutputCacheConfig.IsEnabled)
            {
                List<string> tags = new List<string>();
                if (parametersCollection != null)
                {
                    foreach (string name in parametersCollection.Keys)
                    {
                        if (!string.IsNullOrWhiteSpace(name))
                            tags.Add(string.Format("{0}={1}", name, parametersCollection[name] == null ? "" : parametersCollection[name]));
                    }
                }
                RemoveOutputCacheItem(path, tags.ToArray());
            }
        }
    }
}
